/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package muddb.entities;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;


/**
 *
 * @author keith
 */
@Entity
@Table( name = "user_char_item" )
@IdClass( UserCharItemPK.class )
@NamedQueries(
  { 
    @NamedQuery( name = "UserCharItem.findByCharId",
                  query = "SELECT u FROM UserCharItem u WHERE u.charId = :charId" ),
    @NamedQuery( name = "UserCharItem.findByItemClassId",
                  query = "SELECT u FROM UserCharItem u WHERE u.itemClassId = :itemClassId" ),
    @NamedQuery( name = "UserCharItem.findByItemId",
                  query = "SELECT u FROM UserCharItem u WHERE u.itemId = :itemId" ) } )
public class UserCharItem implements Serializable {
  private static final long serialVersionUID = 1L;

  private int userId;
  private int charId;
  private int itemClassId;
  private int itemId;

  private EquippedLoc equippedLocId;
  private MudItem mudItem;
  private UserChar userChar;


  public UserCharItem() {
  }


  public UserCharItem( int userId,int charId, int itemClassId, int itemId  ) {
    this.userId = userId;
    this.charId = charId;
    this.itemClassId = itemClassId;
    this.itemId = itemId;
  }


  public UserCharItem( UserChar userChar, MudItem mudItem ) {
    this.userChar = userChar;
    this.mudItem = mudItem;
    
    this.userId = userChar.getUserId();
    this.charId = userChar.getCharId();
    this.itemClassId = mudItem.getItemClassId();
    this.itemId = mudItem.getItemId();
  }


  @Id
  @Column( name = "char_id", nullable = false )
  public int getCharId() {
    return charId;
  }


  public void setCharId( int charId ) {
    this.charId = charId;
  }


  @Id
  @Column( name = "user_id", nullable = false )
  public int getUserId() {
    return userId;
  }


  public void setUserId( int userId ) {
    this.userId = userId;
  }


  @Id
  @Column( name = "item_class_id", nullable = false )
  public int getItemClassId() {
    return itemClassId;
  }


  public void setItemClassId( int itemClassId ) {
    this.itemClassId = itemClassId;
  }


  @Id
  @Column( name = "item_id", nullable = false )
  public int getItemId() {
    return itemId;
  }


  public void setItemId( int itemId ) {
    this.itemId = itemId;
  }


  @JoinColumn( name = "equipped_loc_id", referencedColumnName = "equipped_loc_id" )
  @ManyToOne
  public EquippedLoc getEquippedLocId() {
    return equippedLocId;
  }


  public void setEquippedLocId( EquippedLoc equippedLocId ) {
    this.equippedLocId = equippedLocId;
  }


  @JoinColumns( value = {
      @JoinColumn( name = "item_class_id", referencedColumnName = "item_class_id",
                    insertable = false, updatable = false ),
      @JoinColumn( name = "item_id", referencedColumnName = "item_id",
                    insertable = false, updatable = false ) } )
  @ManyToOne
  public MudItem getMudItem() {
    return mudItem;
  }


  public void setMudItem( MudItem mudItem ) {
    this.mudItem = mudItem;
    this.itemClassId = mudItem.getItemClassId();
    this.itemId = mudItem.getItemId();
  }


  @JoinColumns( value = {
      @JoinColumn( name = "user_id", referencedColumnName = "user_id",
                    nullable = false ),
      @JoinColumn( name = "char_id", referencedColumnName = "char_id",
                    insertable = false, updatable = false ) } )
  @ManyToOne
  public UserChar getUserChar() {
    return userChar;
  }


  public void setUserChar( UserChar userChar ) {
    this.userChar = userChar;
    this.userId = userChar.getUserId();
    this.charId = userChar.getCharId();
  }


  @Override
  public int hashCode() {
    int hash = 0;
    hash += (int) charId;
    hash += (int) itemClassId;
    hash += (int) itemId;
    hash += (int) userId;
    return hash;
  }


  @Override
  public boolean equals( Object object ) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if ( !( object instanceof UserCharItem ) ) {
      return false;
    }
    UserCharItem other = (UserCharItem) object;
    if ( this.charId != other.charId ) {
      return false;
    }
    if ( this.itemClassId != other.itemClassId ) {
      return false;
    }
    if ( this.itemId != other.itemId ) {
      return false;
    }
    if ( this.userId != other.userId ) {
      return false;
    }
    return true;
  }


  @Override
  public String toString() {
    return "muddb.entities.UserCharItem[userId=" + userId + ", charId=" + charId + ", itemClassId=" + itemClassId + ", itemId=" + itemId + "]";
  }

}
